package design.pattern.Lu05BuilderPattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @program JavaBase
 * @description: 建造者模式
 * @author: zhanglu
 * @create: 2020-03-28 10:53:00
 */
public class Lu05BuilderPattern2 {

    public static void main(String[] args) {

//        List<String> ops = Arrays.asList("start", "engineBoom", "end");
//
//        Car02Builder01 car02Builder01 = new Car02Builder01();
//        car02Builder01.setOps(ops);
//        car02Builder01.getCar().run();

        Director director = new Director();
        director.getCar01().run();



    }

}

abstract class ICar01{

    protected List<String> ops = new ArrayList<>();

    abstract void name();

    abstract void start();

    abstract void op();

    abstract void end();

    final protected void run(){
        this.name();
        for (String op : ops) {
            if(op.equalsIgnoreCase("start")){
                this.start();
            }else if(op.equalsIgnoreCase("op")){
                this.op();
            }else if(op.equalsIgnoreCase("end")){
                this.end();
            }
        }
    }

    final public void setOps(List<String> ops){
        this.ops = ops;
    }

}

class Car03 extends ICar01{

    @Override
    void name() {
        System.out.println("Car03");
    }

    @Override
    void start() {
        System.out.println("start");
    }

    @Override
    void op() {
        System.out.println("op");
    }

    @Override
    void end() {
        System.out.println("end");
    }

}

abstract class CarBuilder01{

    protected abstract ICar01 getCar();

    protected abstract void setOps(List<String> ops);

}

class Car02Builder01 extends CarBuilder01{

    private ICar01 car01 = new Car03();

    @Override
    protected ICar01 getCar() {
        return this.car01;
    }

    @Override
    protected void setOps(List<String> ops) {
        this.car01.setOps(ops);
    }

}

class Director{

    private List<String> ops = new ArrayList<>();
    private CarBuilder01 carBuilder01 = new Car02Builder01();

    public Car03 getCar01(){
        this.ops.clear();
        this.ops.add("start");
        this.carBuilder01.setOps(this.ops);
        return (Car03) this.carBuilder01.getCar();
    }

    public Car03 getCar02(){
        this.ops.clear();
        this.ops.add("start");
        this.ops.add("op");
        this.carBuilder01.setOps(this.ops);
        this.carBuilder01.getCar().run();
        return (Car03) this.carBuilder01.getCar();
    }

}


